package com.iamk.semanticsegment;

public class SobelOperator {
	private int[][] sobelx = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
	private int[][] sobely = { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };

	int threshold;
	public SobelOperator() {
		threshold = 100;
	}

	public double[][] filter(double[][] channel) {
		double Gx = 0, Gy = 0, G;
		int width = channel.length;
		int height = channel[0].length;
		System.out.println(width + "," + height);
		double[][] b = new double[width][height];
		int[][] sobel = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				for (int k = 0; k < 3; k++) {
					for (int l = 0; l < 3; l++) {
						Gx += (sobel[k][l] * channel[j][i]);
						Gy += (sobel[l][k] * channel[k][l]);
					}
				}
				G = Math.sqrt(Math.pow(Gx, 2) + Math.pow(Gy, 2));
				if (G > threshold) {
					b[j][i] = G;
				} else {
					b[j][i] = 0;
				}
			}
		}
		return b;
	}

}
